﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace TVC_WEB.Global
{
    public partial class User : System.Web.UI.Page
    {
        String sErr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            txtcofirmpass.Attributes["onkeyup"] = "checkpass()";
            if (Request["login"] == "singin")
            {
                Session["Username"] = null;
                Response.Redirect("Login.aspx");
            }
            if (!IsPostBack)
            {
                if (Session["Username"] != null)
                {
                    BUS.UserControl ctr = new BUS.UserControl();
                    DTO.UserInfo inf = ctr.Get_Users_Email(Session["Username"].ToString(), ref sErr);

                    SetDataToForm(inf);
                }

            }

        }

        protected void SetDataToForm(DTO.UserInfo inf)
        {
            txtEmail.Text = inf.Email;
            txtFirstName.Text = inf.FirstName.Trim();
            txtLastName.Text = inf.LastName.Trim();
            txtAddress1.Text = inf.Address1.Trim();
            txtAddress2.Text = inf.Address2.Trim();
            txtCompanyName.Text = inf.CompanyName.Trim();
            txtMobile.Text = inf.Mobile.Trim();
            txtDistrict.Text = inf.District.Trim();
            txtCity.Text = inf.City.Trim();
            country.Value = inf.Country.Trim();
            cbbGender.SelectedValue = inf.Sex.Trim();
            txtPostcode.Text = inf.Postcode.Trim();


        }

        protected DTO.UserInfo GetDataFromForm(DTO.UserInfo inf)
        {

            inf.Email = txtEmail.Text;
            if (txtnewpass.Text != "")
                inf.Password = txtnewpass.Text;
            inf.FirstName = txtFirstName.Text;
            inf.LastName = txtLastName.Text;
            inf.Address1 = txtAddress1.Text;
            inf.Address2 = txtAddress2.Text;
            inf.CompanyName = txtCompanyName.Text;
            inf.Mobile = txtMobile.Text;
            inf.District = txtDistrict.Text;
            inf.City = txtCity.Text;
            inf.Country = country.Value;
            inf.Sex = cbbGender.SelectedValue;

            inf.Postcode = txtPostcode.Text;


            return inf;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (lbErrPass.Text == "")
            {
                BUS.UserControl ctr = new BUS.UserControl();
                DTO.UserInfo inf = ctr.Get_Users_Email(Session["Username"].ToString(), ref sErr);

                inf = GetDataFromForm(inf);
                if (txtnewpass.Text != "")
                {
                    inf.Password = txtnewpass.Text;
                }

                ctr.Update(inf);
            }
        }
    }
}
